package com.suli.multimedia.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.scenic.util.ResponseResult;
import com.suli.multimedia.entity.Article;
import com.suli.multimedia.entity.vo.ArticleVO;
import com.suli.multimedia.service.ArticleContentService;
import com.suli.multimedia.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-03-17
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @Autowired
    ArticleContentService articleContentService;
    /**
     * 创建新闻(NEWS)或通知(ANNOUNCE)
     * @param articleVO 文章内容VO类
     */
    @PostMapping("/create")
    public ResponseResult createArticle(@RequestBody ArticleVO articleVO){
        if(articleService.createOneArticle(articleVO)){
            return ResponseResult.ok().message("创建成功");
        }
        return ResponseResult.error().message("创建失败");
    }

    /**
     * 分页获取文章列表
     * @param page 页码
     * @param limit 数量限制
     * @return 新闻列表
     */
    @GetMapping("/getArticleByPage/{page}/{limit}/{articleType}")
    public ResponseResult getArticleByPage(@PathVariable Long page,
                                           @PathVariable Long limit,
                                           @PathVariable Integer articleType){
        Map<String, Object> articleByPage = articleService.getArticleByPage(page, limit,articleType);
        if(ObjectUtils.isEmpty(articleByPage))
            return ResponseResult.error().message("拉取文章失败");
        return ResponseResult.ok().data(articleByPage);
    }

    /**
     * 根据文章标题获取文章列表
     * @param title 文章标题
     * @param articleType 文章类型
     * @return 文章列表
     */
    @GetMapping("/getArticleByTitle/{articleType}")
    public ResponseResult getArticleByTitle(@RequestParam String title,@PathVariable Integer articleType){
        Map<String, Object> articleByTitle = articleService.getArticleByTitle(title, articleType);
        return  ResponseResult.ok().data(articleByTitle);
    }

    /**
     * 获取文章详情
     * @param articleId 文章id
     * @return 文章数据
     */
    @GetMapping("/getArticleDetail/{articleId}")
    public ResponseResult getArticle(@PathVariable Long articleId){

            Map<String, Object> article = articleService.getArticleById(articleId);
            if(ObjectUtils.isEmpty(article))
                return ResponseResult.error().message("获取失败,文章不存在");
            return ResponseResult.ok().data(article);
    }

    /**
     * 删除一篇文章
     * @param articleId 文章id
     * @return 删除结果
     */
    @DeleteMapping("/deleteArticle/{articleId}")
    public ResponseResult deleteArticle(@PathVariable Integer articleId){
        System.out.println(articleId);
        if(articleService.removeById(articleId)){
            if(articleContentService.removeById(articleId)){
                return ResponseResult.ok().message("删除文章成功");
            }
            return ResponseResult.error().message("删除文章失败");
        }
        return ResponseResult.error().message("删除文章失败");
    }

    /**
     * 批量删除文章
     * @param articleIds id集合
     * @return 删除结果
     */
    @DeleteMapping("/deleteArticles")
    public ResponseResult deleteArticles(@RequestBody List<Integer> articleIds){
        if(articleService.removeByIds(articleIds)){
            if(articleContentService.removeByIds(articleIds)){
                return ResponseResult.ok().message("批量删除成功");
            }
            return ResponseResult.error().message("批量删除失败");
        }
        return ResponseResult.error().message("批量删除失败");
    }

    /**
     * 获取文章列表（被逻辑删除的文章）
     * @param articleType 文章类型
     * @param limit 数量
     * @param page 页码
     * @return 文章列表
     */
    @GetMapping("/getArticleBeDeleted/{page}/{limit}/{articleType}")
    public ResponseResult getArticleList(@PathVariable Integer articleType,
                                         @PathVariable Integer limit,
                                         @PathVariable Integer page){
        Map<String, Object> articleList = articleService.getArticleListDeleted(page, limit, articleType);
        if(ObjectUtils.isEmpty(articleList)){
            return  ResponseResult.error();
        }
        return  ResponseResult.ok().data(articleList);
    }

    /**
     *获取用户写的文章
     * @param userId 用户id
     * @param page 页码
     * @param limit 数量
     * @param articleType 文章类型
     * @return 文章列表
     */
    @GetMapping("/getArticleById/{userId}/{page}/{limit}/{articleType}")
    public ResponseResult getArticleListById(@PathVariable String articleType,
                                             @PathVariable Long limit,
                                             @PathVariable Long page,
                                             @PathVariable String userId){
        Map<String, Object> articleList = articleService.getArticleByUserId(userId,page, limit, articleType);
        if(ObjectUtils.isEmpty(articleList)){
            return  ResponseResult.error();
        }
        return  ResponseResult.ok().data(articleList);
    }

    /**
     * 删除文章
     * @param articleIds 文章id列表
     * @return 删除结果
     */
    @DeleteMapping("/deleteArticleDeleted")
    public ResponseResult deleteArticleBeDeleted(@RequestBody List<Integer> articleIds){
        System.out.println(articleIds);
        articleService.deleteArticleDeleted(articleIds);
            return ResponseResult.ok().message("删除成功");

    }

    /**
     * 获取文章详情(被删除)
     * @param articleId  文章id
     * @return 文章详情
     */
    @GetMapping("/getArticleBeDeleted/{articleId}")
    public ResponseResult getArticleBeDeleted(@PathVariable Integer articleId){
        Map<String, Object> detailDeleted = articleService.getDetailDeleted(articleId);
        return ResponseResult.ok().data(detailDeleted);

    }

    /**
     * 批量还原文章
     * @param ids id列表
     * @return 还原结果
     */
    @PutMapping("/recoverArticle")
    public ResponseResult recoverArticle(@RequestBody List<Integer> ids){
        if(articleService.recovery(ids)){
            return ResponseResult.ok().message("还原成功");
        }
        return ResponseResult.error().message("还原失败");
    }

    /**
     * 更新阅读量
     * @param articleId 文章id
     */
    @PutMapping("/updateView/{articleId}")
    public ResponseResult updateView(@PathVariable Integer articleId){
        UpdateWrapper<Article> updateWrapper=new UpdateWrapper<>();
        updateWrapper.setSql("views=views+1").eq("article_id", articleId);
        articleService.update(updateWrapper);
        return ResponseResult.ok();
    }
}

